/*
XCommander Extension
http://www.xcommander.ch/
*/

function XCommanderBrowser() {

}

XCommanderBrowser.createBrowser = function() {
	//check for opera
	if(window.opera != undefined) {
		XCommanderOpera.prototype = new XCommanderBrowser();
		return new XCommanderOpera();
	}
	// check for google chrome
	else if(window.chrome != undefined && window.chrome.extension != undefined) {
		XCommanderChrome.prototype = new XCommanderBrowser();
		return new XCommanderChrome();
	}
	// check for safari
	else if(window.safari != undefined) {
		XCommanderSafari.prototype = new XCommanderBrowser();
		return new XCommanderSafari();
	} else
		throw 'Unsupported Browser';
}

XCommanderBrowser.prototype = {
	createTab : function(url) {
		throw new XCommanderNotImplementedException('navigateTo');
	},
	navigateTo : function(url) {
		throw new XCommanderNotImplementedException('navigateTo');
	},
	getExtensionUrl : function(relativePath) {
		throw new XCommanderNotImplementedException('getExtensionUrl');
	},
	postMessage : function(request, data, callback) {
		throw new XCommanderNotImplementedException('postMessage');
	}
}

// chrome
function XCommanderChrome() {

	this.createTab = function(url) {
		chrome.tabs.create({
			'url' : url,
			'active' : true
		});
	}

	this.navigateTo = function(url) {
		chrome.tabs.getSelected(null, function(tab) {
			chrome.tabs.update(tab.id, {
				'url' : url
			});
		});
	}

	this.getExtensionUrl = function(relativePath) {
		return chrome.extension.getURL(relativePath);
	}

	this.postMessage = function(request, data, callback) {
		callback(data);
	}

	chrome.extension.onRequest.addListener(function(request, sender, callback) {
		xcommander.lib.messaging.server._onMessage(request, request.type, request.data, callback);
	});
}

// opera
function XCommanderOpera() {

	this.createTab = function(url) {
		opera.extension.tabs.create({
			'url' : url,
			'focused' : true
		});
	}

	this.navigateTo = function(url) {
		this.createTab(url);
	}

	this.getExtensionUrl = function(relativePath) {
		return relativePath;
	}

	this.postMessage = function(request, data, callback) {
		request.source.postMessage({
			'name' : request.data.name,
			'id' : request.data.id,
			'data' : data
		});
	}

	opera.extension.onmessage = function(request) {
		xcommander.lib.messaging.server._onMessage(request, request.data.type, request.data.data, null);
	};
}

// safari
function XCommanderSafari() {

	this.createTab = function(url) {
		var newTab = safari.application.activeBrowserWindow.openTab();
		newTab.url = url;
	}

	this.navigateTo = function(url) {
		var activeTab = safari.application.activeBrowserWindow.activeTab;
		activeTab.url = url;
	}

	this.getExtensionUrl = function(relativePath) {
		return safari.extension.baseURI + relativePath;
	}

	this.postMessage = function(request, data, callback) {
		request.target.page.dispatchMessage(request.name, {
			'id' : request.message.id,
			'data' : data
		});
	}

	safari.application.addEventListener('message', function(request) {
		xcommander.lib.messaging.server._onMessage(request, request.name, request.message.data, null);
	}, false);
}